import { Component, OnInit, ViewChild, ɵɵsetComponentScope } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { SFComponent, SFSchema, SFSelectWidgetSchema, SFTextareaWidgetSchema, SFUISchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { NzModalService } from 'ng-zorro-antd/modal';
import { OrderManagementService } from '../../services/order-management.service';
// import { RiskOrderService } from '../../services/risk-order.service';
// import { CtcAppealComponent } from '../appeal/appeal.component';

@Component({
  selector: 'app-oder-management-component-risk-detail',
  templateUrl: './complaint-detail.component.html',
  styleUrls: ['./complaint-detail.component.less']
})
export class OrderManagementComplaintDetailComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  @ViewChild('sfView', { static: false }) sfView!: SFComponent;
  ui: SFUISchema = {};
  uiView: SFUISchema = {};
  isVisibleRE = false;
  channelId: any;
  schema: SFSchema = {};
  schemaView: SFSchema = {};
  i: any;
  datailList: any = {
    complainantName: '',
    complainantPartyLabel: ''
  };
  complaint: any;
  complaintStatus = false
  id: string = '';
  constructor(private modal: NzModalService, public service: OrderManagementService, public ar: ActivatedRoute) {
    this.id = this.ar.snapshot.params.id;
  }

  ngOnInit(): void {
    if (this.id)
    {
      this.getDetail(this.id);
      this.initSF();
      this.initSTAudit()
    } 
    
  }
  initSTAudit() {
    this.schemaView = {
      properties: {
        handleResult: {
          title: '处理结果',
          type: 'string',
          maxLength: 50,
          ui: {
            placeholder: '最多不超过50字',
            widget: 'textarea',
            autosize: { minRows: 3, maxRows: 6 }
          },
        },
      },
      required: ['handleResult']
    };
    this.uiView = { '*': { spanLabelFixed: 110, grid: { span: 24 } } };
  }
  initSF() {
    this.schema = {
      properties: {
        shpComplaintCauseLabel: {
          title: '投诉原因',
          type: 'string',
          maxLength: 30,
          ui: {
            hidden: this.ar.snapshot.queryParams.sts == 2,
            widget: 'text',
            change: (value, orgData) => console.log(value, orgData),
          } as SFSelectWidgetSchema,
        },
        drvComplaintCauseLabel: {
          title: '投诉原因',
          type: 'string',
          maxLength: 30,
          ui: {
            hidden: this.ar.snapshot.queryParams.sts == 1,
            widget: 'text',
            change: (value, orgData) => console.log(value, orgData),
          } as SFSelectWidgetSchema,
        },
        complainantName: {
          title: '托运方',
          type: 'string',
          maxLength: 30,
          ui: {
            widget: 'text',
            change: (value, orgData) => console.log(value, orgData),
          } as SFSelectWidgetSchema,
        },
        complainantIdLabel: {
          title: '司机',
          type: 'string',
          maxLength: 30,
          ui: {
            widget: 'text',
            change: (value, orgData) => console.log(value, orgData),
          } as SFSelectWidgetSchema,
        },
        complaintDetails: {
          title: '投诉详情',
          type: 'string',
          ui: {
            widget: 'textarea',
            placeholder: '请输入',
            autosize: {
              minRows: 4,
              maxRows: 4
            }
          },
          readOnly: true
        } as SFTextareaWidgetSchema,
        imgUrls: {
          type: 'string',
          title: '上传凭证',
          ui: {
            widget: 'custom'
          }
        },
      },
    };

    this.ui = {
      '*': {
        spanLabelFixed: 180,
        grid: { span: 18 },
        width: 600,
      },
      $title1: {
        spanLabelFixed: 0,
      },
      $title2: {
        spanLabelFixed: 0,
      },
      $title3: {
        spanLabelFixed: 0,
      },
      $unit: {
        spanLabelFixed: 20,
        grid: { span: 3 },
      },
    };
  }
  getDetail(id: string) {
    this.service.request(this.service.$api_get_getComplaintDriverDetails, { id }).subscribe(res => {
      if (res) {
        this.datailList = res;
        this.complaint = JSON.parse(this.ar.snapshot.queryParams.detail)
        this.datailList.complainantName = this.complaint?.shipperAppUserName
        this.datailList.complainantPartyLabel = this.complaint?.driverIdLabel
      }
    })
  }

  edit(item: any): void {
    const modalRef = this.modal.create({
      nzTitle: '申诉',
      nzWidth: '40%',
      // nzContent: CtcAppealComponent,
      nzComponentParams: {
        i: item
      },
      nzFooter: null
    });
    modalRef.afterClose.subscribe(res => {
      if (res) {

      }
    })
  }
  goBack() {
    window.history.go(-1)
  }
 /*
  * 审核关闭弹窗
  view: 1
  浮动费用： 0
  查看评价： 3
  */
  handleCancel(type: string) {
    if(!this.sfView.valid) {
      this.service.msgSrv.error('请填写处理结果！')
      return
    }
    const paramsa = {
      ...this.sfView.value,
      handleStatus: 0,
      id: this.channelId
    }
    this.service.request(this.service.$api_get_dealWithComplaint, paramsa).subscribe((res: any) =>{
      if(res) {
        this.service.msgSrv.success('已取消！')
        this.isVisibleRE = false
        this.getDetail(this.id);
        this.complaintStatus = true;
      } else{
        this.service.msgSrv.error(res?.msg)
      }
    })
    this.isVisibleRE = false
  }
  Cancel() {
    this.isVisibleRE = false
  }
  handleCancel2() {
    if(!this.sfView.valid) {
      this.service.msgSrv.error('请填写处理结果！')
      return
    }
    const paramsa = {
      ...this.sfView.value,
      id: this.channelId
    }
    this.service.request(this.service.$api_get_dealWithComplaint, paramsa).subscribe((res: any) =>{
      if(res) {
        this.service.msgSrv.success('已拒绝！')
        this.isVisibleRE = false
        this.getDetail(this.id);
        this.complaintStatus = true;
      } else{
        this.service.msgSrv.error(res?.msg)
      }
    })
    this.isVisibleRE = false
  }
    /**
  * 审核通过按钮
  */
  handleOK() {
    if(!this.sfView.valid) {
      this.service.msgSrv.error('请填写处理结果！')
      return
    }
    const paramsa = {
      ...this.sfView.value,
      handleStatus: 1,
      id: this.channelId
    }
    this.service.request(this.service.$api_get_dealWithComplaint, paramsa).subscribe((res: any) =>{
      if(res) {
        this.service.msgSrv.success('已通过！')
        this.isVisibleRE = false
        this.complaintStatus = true;
        this.getDetail(this.id);
      } else{
        this.service.msgSrv.error(res?.msg)
      }
    })
  }
  viewEvaluate() {
    this.isVisibleRE = true
    this.channelId = this.id;
  }
}
